package org.example;

import java.sql.*;

//插入操作
public class Jdbc_Teacher_CRUD_Insert {
    static final String URL = "jdbc:mysql://localhost:3306/mysqlbase?serverTimezone=GMT&characterEncoding=UTF-8";
    static final String USERNAME = "root";
    static final String PASSWORD = "1234";
    static final String SQL = "INSERT INTO teacher (id, name, course, birthday) VALUES (?, ?, ?, ?)";

    public static void main(String[] args) throws SQLException {
        try ( Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);) {
            conn.setAutoCommit(false);
            try ( PreparedStatement ps = conn.prepareStatement(SQL);) {
                ps.setInt(1, 11);
                ps.setString(2, "张教授");
                ps.setString(3, "语文");
                ps.setDate(4, Date.valueOf("1990-01-01"));
                ps.executeUpdate();
                conn.commit();
            } catch (SQLException e) {
                conn.rollback();
                e.printStackTrace();
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
